package core;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Resource{
	private BufferedImage image = null;
	private String filename;
	
	public Resource( String _filename ){
		try{
			image = ImageIO.read( new File( "res/" + _filename ) );
		}catch( IOException e ){
			e.printStackTrace();
		}
		
		filename = _filename;
	}
	
	public Resource(){
		filename = "";
	}
	
	public void view(){
		JFrame f = new JFrame( filename );
		f.addWindowListener( new WindowAdapter(){
			public void windowClosing( WindowEvent e ){
				e.getWindow().dispose();
			}
		} );
		Viewer content = new Viewer();
		f.add( content );
		f.pack();
		f.setVisible( true );
		
		System.out.println( "see?" );
	}
	
	// accessors
	public int getType(){ return image.getType(); }
	public int getWidth(){ return image.getWidth(); }
	public int getHeight(){ return image.getHeight(); }
	public int getRGB( int x, int y ){ return image.getRGB( x, y ); }
	public String get_filename(){ return filename; }
	public BufferedImage get_image(){ return image; }
	
	public void set_filename( String _filename ){ filename = _filename; }
	public void set_image( BufferedImage _image ){ image = _image; }
	
	// inner class - for viewer
	class Viewer extends JPanel{
		public void paintComponent( Graphics g ){
			g.drawImage( image, 0, 0, null );
		}
		
		public Dimension getPreferredSize(){
			if( image == null ){
				return new Dimension( 100, 100 );
			}else{
				return new Dimension( image.getWidth(), image.getHeight() );
			}
		}
	}//end-of-class Viewer
	
	// driver: for test purpose
	public static void main( String args[] ){
		Resource r = new Resource( "lena_std.jpg" );
		java.util.ArrayList<Integer> ord = new java.util.ArrayList<Integer>();
		ord.add( filter.FilterModuleList.GRAYSCALE );
		ord.add( filter.FilterModuleList.GBLUR );
		ord.add( filter.FilterModuleList.BINARIZE );
		
		Worklet w = new Worklet( r, ord, 0, 0 );
		r.image = w.get_image();
		r.filename = w.get_name();
		r.view();
		
		// exec
		while( !w.exec() );
		r.image = w.get_image();
		r.filename = w.get_name();
		r.view();
		
		int rgb = r.image.getRGB( 200, 200 );
		System.out.print( "a=" + ( 0x000000FF & ( rgb >> 24 ) ) );
		System.out.print( "r=" + ( ( 0x00FF0000 & rgb ) >> 16 ) );
		System.out.print( "g=" + ( ( 0x0000FF00 & rgb ) >> 8 ) );
		System.out.println( "b=" + ( 0x000000FF & rgb ) );
		System.out.println( r.image.getRGB( 1, 2 ) );
	}//end-of-main
}
